<?php

namespace app\admin\controller;
use app\admin\model\User;
use app\common\controller\Backend;
use think\Controller;
use think\Request;
use think\Db;
use think\Log;

/**
 *
 *
 * @icon fa fa-circle-o
 */
class Parentuser extends Backend
{

    /**
     * UserBank模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Invite');

    }

    public function index()
    {
        //设置过滤方法
        //$this->request->filter(['strip_tags', 'htmlspecialchars']);
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model->with("user")
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)->with("user")
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach($list as $k=>$v){
                $user  = Db::name('user')->field('phone,name')->where('userId',$v['userId'])->find();
                $list[$k]['beinvitePhone'] = $user['phone'];
                $list[$k]['beinviteName'] = $user['name'];

                $first = Db::name('borrow_tender')->where(['userId'=>$v['userId']])->where('status >0 ')->find();
                $total = Db::name('borrow_tender')->field('sum(account) as money')->where(['userId'=>$v['userId']])->where('status >0 ')->find();
                $list[$k]['firstmoney'] = $first['account'];
                $list[$k]['firstcreatetime'] = $first['createTime'];
                $list[$k]['total'] = $total['money'];
            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }


}
